#include "UUTIPC.h"

//////////////////////////////////////////////////////////////////////////
// a simple implementation for the IPC
UUT_START
/*
typedef enum
{
	IPCResult_OK = 0,
	IPCResult_Unknown_Error = 1,
	IPCResult_NULL_Pointer = 2,
	IPCResult_Internel_Error = 3,
	IPCResult_No_Such_Channel = 4
}IPCResult;*/

class IPCClient: public IIPCClient
{
public:
	IPCClient():m_theSink(NULL){};
	virtual ~IPCClient(){ m_theSink = NULL; };

public:
	virtual IPCResult SetCallBack(IIPCSink* callback) 
	{ 
		m_theSink = callback; 
		return IPCResult_OK;
	};
	virtual IPCResult SendData(IPCChannel channel, const UUTByte* data, const UUTUInt32 size, UUTUInt32& sent);
	virtual IPCResult AsyncSendData(IPCChannel channel, const UUTByte* data, const UUTUInt32 size);

private:
	IIPCSink* m_theSink;
};

class IPCService: public IIPCService
{
public:
	IPCService():m_client(NULL), m_myChannel(INVALID_IPC_CHANNEL){ };
	virtual ~IPCService()
	{
		if (m_client) { delete m_client; m_client = NULL; }
	};

public:
	virtual IIPCClient* GetClient(const UUTTString& name);
	virtual IPCResult FindChannel(const UUTTString& name, IPCChannel& channel);

private:
	IIPCClient* m_client;
	IPCChannel  m_myChannel;
	UUTTString	m_myName;
};


//////////////////////////////////////////////////////////////////////////
//
IPCResult IPCClient::SendData(IPCChannel channel, const UUTByte* data, const UUTUInt32 size, UUTUInt32& sent)
{
	return IPCResult_No_Implementation;
};

IPCResult IPCClient::AsyncSendData(IPCChannel channel, const UUTByte* data, const UUTUInt32 size)
{
	return IPCResult_No_Implementation;
};

IIPCClient* IPCService::GetClient(const UUTTString& name)
{
	return NULL;
};

IPCResult IPCService::FindChannel(const UUTTString& name, IPCChannel& channel)
{
	return IPCResult_No_Implementation;
};

static IPCService* s_ipcServiceInstance = NULL;
void InitIPCService()
{
	if (s_ipcServiceInstance) return;		//initialized, just return 
	s_ipcServiceInstance = new IPCService;
};

void TermIPCService()
{
	if (!s_ipcServiceInstance) return;	//not initialized or terminated, just return
	delete s_ipcServiceInstance;
	s_ipcServiceInstance = NULL;
};

IIPCService* GetIPCService()
{
	if (!s_ipcServiceInstance) InitIPCService();
	return s_ipcServiceInstance;
};

UUT_END